﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IDAL;

namespace KPIS.GERP.GFMS.DAL
{
    public class D_BGM_MAS_Option : IDAL_BGM_MAS_Option
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        ConvertNullable cn = new ConvertNullable();

        public IList<M_BGM_MAS_Option> GetFull(Nullable<int> bgm_option_type_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_BGM_MAS_Option> infoList = new List<M_BGM_MAS_Option>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@bgm_option_type_seq", bgm_option_type_seq);

                strSQL = "SELECT op.*"
                    + ", opt.bgm_option_type_name"
                    + " FROM bgm_mas_option op"
                    + " LEFT JOIN bgm_system_option_type opt ON op.bgm_exp_grp_seq = opt.bgm_exp_grp_seq"
                    + " WHERE op.record_status not like @delete_status"
                    + (bgm_option_type_seq != null ? " and op.bgm_option_type_seq = @bgm_option_type_seq" : "");

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_BGM_MAS_Option info = new M_BGM_MAS_Option();
                        info.bgm_option_seq = Convert.ToInt32(drc[i]["bgm_option_seq"]);
                        info.bgm_option_code = drc[i]["bgm_exp_code"].ToString();
                        info.bgm_option_name = drc[i]["bgm_exp_name"].ToString();
                        info.bgm_option_descr = drc[i]["bgm_exp_descr"].ToString();
                        info.bgm_option_type_seq = cn.ConvertNullInt(drc[i]["bgm_option_type_seq"]);
                        info.upper_bgm_option_seq = cn.ConvertNullInt(drc[i]["upper_bgm_option_seq"]);
                        info.m_bgm_system_optiontype.bgm_option_type_name = drc[i]["bgm_option_type_name"].ToString();
                        info.record_status = drc[i]["record_status"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_BGM_MAS_Option> GetList(Nullable<int> bgm_option_type_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_BGM_MAS_Option> infoList = new List<M_BGM_MAS_Option>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@bgm_option_type_seq", bgm_option_type_seq);

                strSQL = "SELECT op.bgm_option_seq, op.bgm_option_name"
                    + " FROM bgm_mas_option op"
                    + " WHERE op.record_status not like @delete_status"
                    + (bgm_option_type_seq != null ? " and op.bgm_option_type_seq = @bgm_option_type_seq" : "");

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_BGM_MAS_Option info = new M_BGM_MAS_Option();
                        info.bgm_option_seq = Convert.ToInt32(drc[i]["bgm_option_seq"]);
                        info.bgm_option_name = drc[i]["bgm_option_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_BGM_MAS_Option> GetIntegrateProjectList(Nullable<int> job_site_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_BGM_MAS_Option> infoList = new List<M_BGM_MAS_Option>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@bgm_option_type_seq", SystemInfo._IntegrateProjectBgmOptionTypeSeq);
                dbManager.AddParameters(2, "@job_site_seq", "%/" + job_site_seq + "/%");

                strSQL = "SELECT -1 AS bgm_option_seq, null AS bgm_option_code, null AS bgm_option_name"
                    + ", 'โครงการภายใต้กิจกรรม' AS bgm_option_text, 0 AS remain_amount"

                    + " UNION ALL"

                    + " SELECT opt.bgm_option_seq, opt.bgm_option_code, opt.bgm_option_name"
                    + ", concat(coalesce(opt.bgm_option_code, ''), ' : ', coalesce(opt.bgm_option_name, '')) as bgm_option_text"

                    + ", 0 as remain_amount"

                    + " FROM bgm_mas_option opt"
                    + " LEFT JOIN hrm_mp_job_sites j ON opt.job_site_seq = j.job_site_seq"
                    //+ " LEFT JOIN bgm_project_option popt ON opt.bgm_option_seq = popt.bgm_option_seq"
                    //    + " AND popt.record_status not like @delete_status"
                    //+ " LEFT JOIN bgm_project_operation op ON popt.bgm_proj_seq = op.bgm_proj_seq"
                    //    + " AND po.record_status not like @delete_status"
                //    + " LEFT JOIN (select opt.bgm_option_seq"
                //        + ", sum(coalesce(opexp.amount, 0)) as oper_amount"
                //        + ", sum()"
                
                //bgm_project_operation_expense opexp ON op.bgm_proj_oper_seq = opexp.bgm_proj_oper_seq"
                        //+ " AND opexp.record_status not like @delete_status"
                    + " WHERE opt.record_status not like @delete_status"
                    + " and opt.bgm_option_type_seq = @bgm_option_type_seq"
                    + (job_site_seq != null ? " and j.job_site_full_path like @job_site_seq" : "");

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_BGM_MAS_Option info = new M_BGM_MAS_Option();
                        info.bgm_option_seq = Convert.ToInt32(drc[i]["bgm_option_seq"]);
                        info.bgm_option_name = drc[i]["bgm_option_name"].ToString();
                        info.bgm_option_text = drc[i]["bgm_option_text"].ToString();
                        info.remain_amount = cn.ConvertNullDouble(drc[i]["remain_amount"]);
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_BGM_MAS_Option> GetListFromBgmProjSeq(Nullable<int> bgm_option_type_seq
            , Nullable<int> bgm_proj_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_BGM_MAS_Option> infoList = new List<M_BGM_MAS_Option>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@bgm_option_type_seq", bgm_option_type_seq);
                dbManager.AddParameters(2, "@bgm_proj_seq", bgm_proj_seq);

                strSQL = "SELECT op.bgm_option_seq, op.bgm_option_name"
                    + " FROM bgm_project_option rop"
                    + " LEFT JOIN bgm_mas_option op ON rop.bgm_option_seq = op.bgm_option_seq"
                    + " WHERE op.record_status not like @delete_status"
                    + (bgm_option_type_seq != null ? " and op.bgm_option_type_seq = @bgm_option_type_seq" : "")
                    + (bgm_proj_seq != null ? " and rop.bgm_proj_seq = @bgm_proj_seq" : "");

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_BGM_MAS_Option info = new M_BGM_MAS_Option();
                        info.bgm_option_seq = Convert.ToInt32(drc[i]["bgm_option_seq"]);
                        info.bgm_option_name = drc[i]["bgm_option_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public M_BGM_MAS_Option GetInfo(int seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_BGM_MAS_Option info = new M_BGM_MAS_Option();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@seq", seq);

                strSQL = "SELECT op.*"
                    + ", opt.bgm_option_type_name"
                    + " FROM bgm_mas_option op"
                    + " LEFT JOIN bgm_system_option_type opt ON op.bgm_exp_grp_seq = opt.bgm_exp_grp_seq"
                    + " WHERE op.bgm_option_seq = @seq";
                    

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.bgm_option_seq = Convert.ToInt32(drc[0]["bgm_option_seq"]);
                    info.bgm_option_code = drc[0]["bgm_exp_code"].ToString();
                    info.bgm_option_name = drc[0]["bgm_exp_name"].ToString();
                    info.bgm_option_descr = drc[0]["bgm_exp_descr"].ToString();
                    info.bgm_option_type_seq = cn.ConvertNullInt(drc[0]["bgm_option_type_seq"]);
                    info.upper_bgm_option_seq = cn.ConvertNullInt(drc[0]["upper_bgm_option_seq"]);
                    info.m_bgm_system_optiontype.bgm_option_type_name = drc[0]["bgm_option_type_name"].ToString();
                    info.record_status = drc[0]["record_status"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }
    }
}
